package com.castle.fortress.admin.orders.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.orders.entity.GoodsOrderItemEntity;
import com.castle.fortress.admin.orders.dto.GoodsOrderItemDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.orders.service.GoodsOrderItemService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单详情 api 控制器
 *
 * @author Mgg
 * @since 2023-01-13
 */
@Api(tags="订单详情api管理控制器")
@RestController
@RequestMapping("/api/orders/goodsOrderItem")
public class ApiGoodsOrderItemController {
    @Autowired
    private GoodsOrderItemService goodsOrderItemService;


    /**
     * 订单详情的分页展示
     * @param goodsOrderItemDto 订单详情实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="订单详情-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("订单详情分页展示")
    @GetMapping("/page")
    public RespBody<IPage<GoodsOrderItemDto>> pageGoodsOrderItem(GoodsOrderItemDto goodsOrderItemDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<GoodsOrderItemDto> page = new Page(pageIndex, pageSize);

        IPage<GoodsOrderItemDto> pages = goodsOrderItemService.pageGoodsOrderItem(page, goodsOrderItemDto);
        return RespBody.data(pages);
    }

    /**
     * 订单详情保存
     * @param goodsOrderItemDto 订单详情实体类
     * @return
     */
    @CastleLog(operLocation="订单详情-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("订单详情保存")
    @PostMapping("/save")
    public RespBody<String> saveGoodsOrderItem(@RequestBody GoodsOrderItemDto goodsOrderItemDto){
        if(goodsOrderItemDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsOrderItemEntity goodsOrderItemEntity = ConvertUtil.transformObj(goodsOrderItemDto,GoodsOrderItemEntity.class);
        if(goodsOrderItemService.save(goodsOrderItemEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 订单详情编辑
     * @param goodsOrderItemDto 订单详情实体类
     * @return
     */
    @CastleLog(operLocation="订单详情-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("订单详情编辑")
    @PostMapping("/edit")
    public RespBody<String> updateGoodsOrderItem(@RequestBody GoodsOrderItemDto goodsOrderItemDto){
        if(goodsOrderItemDto == null || goodsOrderItemDto.getId() == null || goodsOrderItemDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsOrderItemEntity goodsOrderItemEntity = ConvertUtil.transformObj(goodsOrderItemDto,GoodsOrderItemEntity.class);
        if(goodsOrderItemService.updateById(goodsOrderItemEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 订单详情删除
     * @param ids 订单详情id集合
     * @return
     */
    @CastleLog(operLocation="订单详情-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("订单详情删除")
    @PostMapping("/delete")
    public RespBody<String> deleteGoodsOrderItem(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsOrderItemService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 订单详情详情
     * @param id 订单详情id
     * @return
     */
    @CastleLog(operLocation="订单详情-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("订单详情详情")
    @GetMapping("/info")
    public RespBody<GoodsOrderItemDto> infoGoodsOrderItem(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsOrderItemEntity goodsOrderItemEntity = goodsOrderItemService.getById(id);
            GoodsOrderItemDto goodsOrderItemDto = ConvertUtil.transformObj(goodsOrderItemEntity,GoodsOrderItemDto.class);
        return RespBody.data(goodsOrderItemDto);
    }


}
